<?php

class ProjectsController extends AppController {
	
	var $name = 'Projects';
	
	function index() {
		
	}
	
	function add() {
		if(!empty($this->data)) {			
			if($this->Project->save($this->data)) {
				$this->Session->setFlash('Your project has been created');
				$this->redirect(array('controller' => 'projects', 'action' => 'view'));
			}
		} else if(!$this->Session->check('user_data')) {
			$this->Session->setFlash('Please login to continue');
			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		} else {
			$this->set('user_data', $this->Session->read('user_data'));
		}
	}
	
	function string_to_filename($word) {
	    $tmp = preg_replace('/^\W+|\W+$/', '', $word); // remove all non-alphanumeric chars at begin & end of string
	    $tmp = preg_replace('/\s+/', '_', $tmp); // compress internal whitespace and replace with _
	    return strtolower(preg_replace('/\W-/', '', $tmp)); // remove all non-alphanumeric chars except _ and -
	}
	
	
	// Test benchmark generation action
	// Proof of concept - a bit hacky
	
	function generateTest() {
		
		if(!empty($this->data)) {
			$filename = $this->string_to_filename($this->data['title']);
			$filename = $filename . "_SEQ_" . date('YmdHis');
			$output = array();
			exec("./bm2 " . $this->data['unit'] . " " . $this->data['min'] . " " . $this->data['max'] . " " . $this->data['step'], $output);
			// shell_exec("cd ../PROJECTS/webUI/test/");
			// shell_exec("mkdir benchmarks");
			// shell_exec("mv * benchmarks/");
			exec("mv ../PROJECTS/webUI/test/ ../".$filename);
			exec("zip -r ./app/webroot/benchmarks/".$filename . ".zip ../".$filename);
			exec('mkdir ../PROJECTS/webUI/test');
			$this->set("filename", $filename);
		} 
		 
		
		// else if(!$this->Session->check('user_data')) {
		// 			$this->Session->setFlash('Please login to continue');
		// 			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		// 		} else {
		// 			$this->set('user_data', $this->Session->read('user_data'));
		// 		}
		
	}
	
	function generateSpec() {
		if(!empty($this->data)) {
			$this->set('data', $this->data);
		}
	}
	
	function view($id = null) {
		if(is_null($id)) {
			$this->set('projects', $this->Project->find('all'));
		} else {
			$this->set('project', $this->Project->findById($id));
		}
	}
	
	function manageKernels() {
		$kernel_folder_contents = array();
		$handler = opendir(WWW_ROOT."kernel_library/");
		while($file = readdir($handler)) {
			if(!(($file == '.') || ($file == '..') || ($file == '.DS_Store'))) {
				$kernel_folder_contents[] = $file;
			}
		}
		closedir($handler);
		$this->set('kernel_folder_contents', $kernel_folder_contents);
	}
	
	function uploadKernel() {
		
	}
	
	function uploadKernelFile() {
		$target = WWW_ROOT."kernel_library/";
		$target = $target . basename( $_FILES['uploaded']['name']);
		$ok=1; 
		if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
		{
			$this->Session->setFlash("The file <strong>". basename( $_FILES['uploaded']['name']). "</strong> has been uploaded");
			$this->redirect(array('controller' => 'projects', 'action' => 'manageKernels'));
		} 
		else {
			$this->Session->setFlash("Sorry, there was a problem uploading your file.");
			$this->redirect(array('controller' => 'projects', 'action' => 'uploadKernel'));
	 	}
	}
	
}

?>